
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;

@WebServlet("/test2")
public class test2 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 模拟数据
        List<String> categories = new ArrayList<>();
        List<Integer> values = new ArrayList<>();
        categories.add("A");
        categories.add("B");
        categories.add("C");
        values.add(20);
        values.add(30);
        values.add(40);
        Data test = new Data();
        System.out.print(test);

        // 将数据转换为JSON格式
        String jsonData = JSON.toJSONString(new Data());

        // 设置响应内容类型为JSON
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        // 将JSON数据写入响应
        response.getWriter().write(jsonData);
    }

    // 数据类
    class Data {
    	 List<String> categories;
    	 List<Integer> values;

    	 public List<String> getCategories() {
    	     return categories;
    	 }

    	 public void setCategories(List<String> categories) {
    	     this.categories = categories;
    	 }

    	 public List<Integer> getValues() {
    	     return values;
    	 }

    	 public void setValues(List<Integer> values) {
    	     this.values = values;
    	 }
        
    }
}
